package jacidco;

import javax.swing.JOptionPane;


public class CreaUsuarioFrame extends javax.swing.JFrame
{
    private int cod;
    //private char clave[];
    /** Creates new form CreaUsuarioFrame */
    public CreaUsuarioFrame()
    {
        initComponents();

        /* Muestra el siguiente codigo a crear */
        Main.bd.conectar();
        cod = (Main.bd.nextCode("cod_usuario", "usuarios")+1);
        Main.bd.cerrarConexion();
        txtCodUsuario.setText(""+cod);
        /* End */


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTitulo = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        lblCodUsuario = new javax.swing.JLabel();
        txtCodUsuario = new javax.swing.JTextField();
        lblNombreUsuario = new javax.swing.JLabel();
        txtNombreUsuario = new javax.swing.JTextField();
        lblApellidoUsuario = new javax.swing.JLabel();
        txtApellidoUsuario = new javax.swing.JTextField();
        txtUser = new javax.swing.JTextField();
        lblUser = new javax.swing.JLabel();
        lblClave = new javax.swing.JLabel();
        txtClave = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        lblTitulo2 = new javax.swing.JLabel();
        lblMarcareg = new javax.swing.JLabel();
        btmCrear = new javax.swing.JButton();
        btnNuevo = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Crear Usuario");

        lblTitulo.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
        lblTitulo.setText("Comunidad Dinamica en Java");
        lblTitulo.setName("lblTitulo"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setName("jPanel1"); // NOI18N

        lblCodUsuario.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        lblCodUsuario.setText("Codigo Usuario:");
        lblCodUsuario.setName("lblCodUsuario"); // NOI18N

        txtCodUsuario.setEditable(false);
        txtCodUsuario.setName("txtCodUsuario"); // NOI18N

        lblNombreUsuario.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblNombreUsuario.setText("Nombre :");
        lblNombreUsuario.setName("lblNombreUsuario"); // NOI18N

        txtNombreUsuario.setName("txtNombreUsuario"); // NOI18N

        lblApellidoUsuario.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        lblApellidoUsuario.setText("Apellido :");
        lblApellidoUsuario.setName("lblApellidoUsuario"); // NOI18N

        txtApellidoUsuario.setName("txtApellidoUsuario"); // NOI18N

        txtUser.setName("txtUser"); // NOI18N

        lblUser.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        lblUser.setText("User :");
        lblUser.setName("lblUser"); // NOI18N

        lblClave.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        lblClave.setText("Calve :");
        lblClave.setName("lblClave"); // NOI18N

        txtClave.setName("txtClave"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblCodUsuario)
                    .addComponent(lblNombreUsuario)
                    .addComponent(lblUser))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtCodUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNombreUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtUser, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE))
                        .addGap(14, 14, 14)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblClave)
                            .addComponent(lblApellidoUsuario))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtApellidoUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                            .addComponent(txtClave, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE))
                        .addGap(64, 64, 64))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCodUsuario)
                    .addComponent(txtCodUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNombreUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNombreUsuario)
                    .addComponent(txtApellidoUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblApellidoUsuario))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUser)
                    .addComponent(txtUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblClave)
                    .addComponent(txtClave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        jLabel1.setBackground(new java.awt.Color(255, 51, 51));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jacidco/resources/Logo_Jacidco_mini.png"))); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        lblTitulo2.setFont(new java.awt.Font("Times New Roman", 3, 36)); // NOI18N
        lblTitulo2.setText("Usuarios");
        lblTitulo2.setName("lblTitulo2"); // NOI18N

        lblMarcareg.setFont(new java.awt.Font("Times New Roman", 0, 11)); // NOI18N
        lblMarcareg.setText("Copyright 2011. DCM. All Rights Reserved");
        lblMarcareg.setName("lblMarcareg"); // NOI18N

        btmCrear.setFont(new java.awt.Font("Tahoma", 3, 11));
        btmCrear.setText("Crear");
        btmCrear.setName("btmCrear"); // NOI18N
        btmCrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btmCrearActionPerformed(evt);
            }
        });

        btnNuevo.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        btnNuevo.setText("Nuevo");
        btnNuevo.setName("btnNuevo"); // NOI18N
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnCancelar.setFont(new java.awt.Font("Tahoma", 3, 11));
        btnCancelar.setText("Cancelar");
        btnCancelar.setName("btnCancelar"); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblTitulo2, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblTitulo, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btnNuevo)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btmCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnCancelar))
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lblMarcareg))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblTitulo)
                        .addGap(67, 67, 67)
                        .addComponent(lblTitulo2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btmCrear)
                    .addComponent(btnNuevo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblMarcareg)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btmCrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btmCrearActionPerformed


        try
        {
            
            Main.usuario.setCod(Byte.parseByte(txtCodUsuario.getText()));
            Main.usuario.setNombre(txtNombreUsuario.getText());
            Main.usuario.setApellido(txtApellidoUsuario.getText());
            Main.usuario.setUsuario(txtUser.getText());
            Main.usuario.setClave(txtClave.getText());
            
            if(!Main.usuario.getNombre().equals("") && !Main.usuario.getApellido().equals("") && !Main.usuario.getUsuario().equals("") && !Main.usuario.getClave().equals(""))
            {
                Main.bd.conectar();
                if(Main.bd.guardaDatodUsuario(Main.usuario.getCod(), Main.usuario.getNombre(), Main.usuario.getApellido(), Main.usuario.getUsuario(), Main.usuario.getClave()))
                {
                    JOptionPane.showMessageDialog(null, "Datos guardados correctamente", "Informacion", JOptionPane.INFORMATION_MESSAGE);
                }
                Main.bd.cerrarConexion();

                txtNombreUsuario.setEnabled(false);
                txtApellidoUsuario.setEditable(false);
                txtUser.setEnabled(false);
                txtClave.setEnabled(false);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "No dejar campos en blanco", "Informacion", JOptionPane.WARNING_MESSAGE);
            }

        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Error\n\n Detalles"+ex, "Error", JOptionPane.ERROR_MESSAGE);
        }
        

    }//GEN-LAST:event_btmCrearActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        
        Main.bd.conectar();
        cod = (Main.bd.nextCode("cod_usuario", "usuarios")+1);
        Main.bd.cerrarConexion();
        txtCodUsuario.setText(""+cod);
        
        txtNombreUsuario.setText("");
        txtApellidoUsuario.setText("");
        txtUser.setText("");
        txtClave.setText("");
        
        txtNombreUsuario.setEnabled(true);
        txtApellidoUsuario.setEditable(true);
        txtUser.setEnabled(true);
        txtClave.setEnabled(true);
        

    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed

        Main.bd.conectar();
        if(Main.bd.nextCode("cod_usuario", "usuarios") <= 0)
        {
            LoginFrame logIn = new LoginFrame();
            logIn.setVisible(true);
            logIn.setLocationRelativeTo(null);
            dispose();
        }
        else
        {
            MenuPrincipalFrame mp = new MenuPrincipalFrame();
            mp.setVisible(true);
            mp.setLocationRelativeTo(null);
            dispose();
        }
        Main.bd.cerrarConexion();

    }//GEN-LAST:event_btnCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btmCrear;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblApellidoUsuario;
    private javax.swing.JLabel lblClave;
    private javax.swing.JLabel lblCodUsuario;
    private javax.swing.JLabel lblMarcareg;
    private javax.swing.JLabel lblNombreUsuario;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JLabel lblTitulo2;
    private javax.swing.JLabel lblUser;
    private javax.swing.JTextField txtApellidoUsuario;
    private javax.swing.JPasswordField txtClave;
    private javax.swing.JTextField txtCodUsuario;
    private javax.swing.JTextField txtNombreUsuario;
    private javax.swing.JTextField txtUser;
    // End of variables declaration//GEN-END:variables

        public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CreaUsuarioFrame().setVisible(true);
            }
        });
    }
}
